package com.lanou.dllo.soccernews.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;

import com.lanou.dllo.soccernews.R;
import com.lanou.dllo.soccernews.fragment.CircleshowFragment;
import com.lanou.dllo.soccernews.fragment.DatasFragment;
import com.lanou.dllo.soccernews.fragment.DirectroomFragment;
import com.lanou.dllo.soccernews.fragment.HomePageFragment;

public class MainActivity extends ActionBarActivity

        implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {
    private TabHost mhost;
    private ImageView logLogin;
    private DrawerLayout drawer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        logLogin = (ImageView) navigationView.findViewById(R.id.log_login);
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        navigationView.setNavigationItemSelectedListener(this);
        logLogin.setOnClickListener(this);
        initTab();
        initTabView();
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camara) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }


        drawer.closeDrawer(GravityCompat.START);
        return true;
    }


    //tab
    private void initTab() {
        mhost = (TabHost) findViewById(android.R.id.tabhost);
        mhost.setup();

        //添加页面
        TabHost.TabSpec home_page = mhost.newTabSpec("home_page");
        home_page.setIndicator("首页");
        home_page.setContent(R.id.home_page);
        mhost.addTab(home_page);

        TabHost.TabSpec direct_room = mhost.newTabSpec("direct_room");
        direct_room.setIndicator("直播");
        direct_room.setContent(R.id.direct_room);
        mhost.addTab(direct_room);

        TabHost.TabSpec view3 = mhost.newTabSpec("view3");
        view3.setIndicator("+++");
        view3.setContent(R.id.view3);
        mhost.addTab(view3);

        TabHost.TabSpec circle_show = mhost.newTabSpec("circle_show");
        circle_show.setIndicator("圈子");
        circle_show.setContent(R.id.circle_show);
        mhost.addTab(circle_show);

        TabHost.TabSpec datas = mhost.newTabSpec("datas");
        datas.setIndicator("数据");
        datas.setContent(R.id.datas);
        mhost.addTab(datas);

    }

    private void initTabView() {
        FragmentManager fManager = getSupportFragmentManager();
        FragmentTransaction fT = fManager.beginTransaction();
        fT.replace(R.id.home_page, new HomePageFragment());
        fT.replace(R.id.circle_show, new CircleshowFragment());
        fT.replace(R.id.direct_room, new DirectroomFragment());
        fT.replace(R.id.datas, new DatasFragment());

//


        fT.commit();


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.log_login:
                Intent intent = new Intent(this, LoginAndRegist.class);
                startActivity(intent);
        }
//        drawer.closeDrawer(GravityCompat.START);
    }
}
